package com.syncomponents.remoting;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;

import com.syncomponents.context.SynFactory;
import com.syncomponents.core.NotSupportedRecordException;

/**
 * 客户端工廠
 * 
 * @author sunkey
 * @date Mar 12, 2014
 */
@SuppressWarnings("rawtypes")
public class ClientFactory implements SynFactory<IClient> {

    private final static Map<String, IClient> containers = new ConcurrentHashMap<String, IClient>();

    public int getSize() {
        return containers.size();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.syncomponents.context.SynFactory#register(java.lang.String, com.syncomponents.client.IClient)
     */
    @Override
    public void register(String key, IClient value) {
        containers.put(key, value);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.syncomponents.context.SynFactory#getBean(java.lang.String)
     */
    @Override
    public IClient getBean(String pathName) {
        if (StringUtils.isBlank(pathName) || !containers.containsKey(pathName.toLowerCase())) {
            throw new NotSupportedRecordException(pathName + " not found");
        }
        return containers.get(pathName.toLowerCase());
    }

    @Override
    public boolean contains(String pathName) {
        return containers.containsKey(pathName);
    }
}
